package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.youzan.spiderman.utils.StringUtils;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.model.network.dzbean.res.PreAuthResult;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.PayPwdSetOrModifyMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.PayPwdSetOrModifyPresenter;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.widget.GlobalVariables;

/**
 * Created by chensf on 17/10/1.
 */

public class PayPwdSetActivity extends AppBaseActivity implements PayPwdSetOrModifyMvpView {

    EditText et_qr_old_pwd;
    EditText et_qr_new_pwd;
    EditText et_qr_new_pwd2;
    TextView tv_qr_ok;

    private boolean firstSet = false;

    private boolean isMain;

    @Inject
    PayPwdSetOrModifyPresenter mPayPwdSetOrModifyPresenter;

    @Override
    protected CharSequence getTopTitle() {
        firstSet = getIntent().getBooleanExtra("type", false);
        isMain = getIntent().getBooleanExtra("MainFragment", false);
        L.e(Constant.TAG, "是否是首页：" + isMain);
        if (firstSet) {
            return getString(R.string.qr_set_pwd);
        } else {
            return getString(R.string.qr_modify_pwd);
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.qr_activity_paypwdset);

        et_qr_old_pwd = findViewById(R.id.et_qr_old_pwd);
        et_qr_new_pwd= findViewById(R.id.et_qr_new_pwd);
        et_qr_new_pwd2= findViewById(R.id.et_qr_new_pwd2);
        tv_qr_ok= findViewById(R.id.tv_qr_ok);
        tv_qr_ok.setOnClickListener(this::onClickView);

        if (firstSet) {
            et_qr_old_pwd.setVisibility(View.GONE);
        } else {
            et_qr_old_pwd.setVisibility(View.VISIBLE);
        }
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{mPayPwdSetOrModifyPresenter};
    }


    public void onClickView(View v) {
        if (R.id.tv_qr_ok == v.getId()) {
            String oldPwd = et_qr_old_pwd.getText().toString();
            String pwd = et_qr_new_pwd.getText().toString();
            String confirmPwd = et_qr_new_pwd2.getText().toString();
            if (!firstSet && TextUtils.isEmpty(oldPwd)) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, getString(R.string.qr_old_pwd_empty));
                return;
            }
            if (!firstSet && oldPwd.length() != 6) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, "原密码位数不对");
                return;
            }
            if (StringUtils.isEmpty(pwd)) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, getString(R.string.qr_pwd_empty));
                return;
            }
            if (pwd.length() != 6) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, "新密码位数不对");
                return;
            }
            if (StringUtils.isEmpty(confirmPwd)) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, getString(R.string.qr_confirm_pwd_empty));
                return;
            }
            if (confirmPwd.length() != 6) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, "确认密码位数不对");
                return;
            }
            if (!pwd.equals(confirmPwd)) {
                PubUtils.popTipOrWarn(PayPwdSetActivity.this, "两次输入的密码不匹配");
                return;
            }
            if (firstSet) {
                mPayPwdSetOrModifyPresenter.setPayPassword(pwd);
            } else {
                mPayPwdSetOrModifyPresenter.modifyPayPassword(oldPwd, pwd);
            }

        }
    }

    @Override
    public void onPayPwdSetSuccess() {
        PubUtils.popTipOrWarn(PayPwdSetActivity.this, getString(R.string.qr_pwd_set_success));
        PreAuthResult preAuthResult = GlobalVariables.getInstance(this).getPreAuthResult();
        preAuthResult.setUserStatus(Constant.QUERY_QR_RESULT_AUTHED);
        GlobalVariables.getInstance(this).setPreAuthResult(this, preAuthResult);
        if (isMain) {
            eventBus.post(new MessageEvent(MessageEvent.CODE_REFRESH));
            PubUtils.popTipOrWarn(PayPwdSetActivity.this, "首页设置密码完成");
//            Intent intent = new Intent();
//            intent.setAction("realNameSucc");
//            intent.putExtra("realNameSucc", "realNameSucc");
//            sendBroadcast(intent);
        } else {
            eventBus.post(new MessageEvent(MessageEvent.CODE_PASSWORD));
            eventBus.post(new MessageEvent(MessageEvent.CODE_REFRESH_CODE));
            PubUtils.popTipOrWarn(PayPwdSetActivity.this, "非首页设置密码完成");
        }
        finish();
    }

    @Override
    public void onPayPwdSetFailure() {
        //TODO 到某个页面中去
        PubUtils.popTipOrWarn(PayPwdSetActivity.this, getString(R.string.qr_pwd_set_failure));
        et_qr_new_pwd2.setText("");
        et_qr_new_pwd.setText("");
    }

    @Override
    public void onPayPwdModifySuccess() {
        PubUtils.popTipOrWarn(PayPwdSetActivity.this, "支付密码修改成功!");
        eventBus.post(new MessageEvent(MessageEvent.CODE_PASSWORD));
        finish();
    }

    @Override
    public void onPayPwdModifyFail() {
        PubUtils.popTipOrWarn(PayPwdSetActivity.this, "支付密码修改失败!");
        et_qr_new_pwd2.setText("");
        et_qr_new_pwd.setText("");
        et_qr_old_pwd.setText("");
    }
}
